#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tornado.web import RequestHandler
import pymongo
import re
import render
import codecs

_encoding = 'utf-8'
_errors='ignore'
color = ['AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE',]
client_pid = {}


class Daily(RequestHandler):
    def get(self):
        _time = self.get_argument('time', '').strip()
        _type = self.get_argument('type', '').strip()
        _ver = self.get_argument('version', '').strip()

        if not _time:
            _time = '201111'
        
        if not _type:
            _type = 'all'
        
        if not _ver:
            _ver = 'v2'
            
        connection = pymongo.Connection("10.103.13.42", 27017)
        if _ver == 'v2':
            db = connection.iPadV2
        else:
            db = connection.iPadV214
        collection = db[_time]
        posts = collection.find() 

        _posts = []
        pv = {}
        uv = {}
        vv = {}
        new_user = {}
        startuv = {}

        for post in posts:

            pv[post['date']] = pv.get(post['date'], 0) + int(post.get('pv',0))
            uv[post['date']] = uv.get(post['date'], 0) + int(post.get('uv',0))
            vv[post['date']] = vv.get(post['date'], 0) + int(post.get('vv',0))
            new_user[post['date']] = new_user.get(post['date'], 0) + int(post.get('new_user',0))
            startuv[post['date']] = startuv.get(post['date'], 0) + int(post.get('loginuv',0))
           
        
        pad_list = list(pv)
        pad_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 iPad V2.0 {2} 报表图' xAxisName='日期' yAxisName='访问量' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:],_type)
        xml_categories = '<categories>'
        
        print pad_list
        
        for i in range(len(pad_list)):
            xml_categories += "<category Name='{0}' />".format((pad_list[i])[6:])
        xml_categories += '</categories>'
        
        print pad_list
        xml_dataset1 = ''
        if _type == 'all' or _type == 'pv':
            xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
            for i in range(len(pad_list)):
                xml_dataset1 += "<set value='{0}' />".format(pv[pad_list[i]])
            xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ''
        if _type == 'all' or _type == 'uv':
            xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
            for i in range(len(pad_list)):
                xml_dataset2 += "<set value='{0}' />".format(uv[pad_list[i]])
            xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ''
        if _type == 'all' or _type == 'vv':
            xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
            for i in range(len(pad_list)):
                xml_dataset3 += "<set value='{0}' />".format(vv[pad_list[i]])
            xml_dataset3 += "</dataset>"
        
        xml_dataset4 = ''
        if _type == 'all' or _type == 'new_user':
            xml_dataset4 = ur"<dataset seriesName='new_user' color='FF8E46'>"
            for i in range(len(pad_list)):
                xml_dataset4 += "<set value='{0}' />".format(new_user[pad_list[i]])
            xml_dataset4 += "</dataset>"
        
        xml_dataset5 = ''
        if _type == 'all' or _type == 'startuv':
            xml_dataset5 = ur"<dataset seriesName='startuv' color='FF8E46'>"
            for i in range(len(pad_list)):
                xml_dataset5 += "<set value='{0}' />".format(startuv[pad_list[i]])
            xml_dataset5 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_dataset4+xml_dataset5+xml_footer
        
        pad_list.sort(None,None,True)
        
        self.render('templates/client-ipad-daily.html', keys=pad_list, version=_ver, time=_time, type=_type, pv=pv, uv=uv, vv=vv, new_user=new_user, startuv=startuv, xmlStr=xmlStr)
